/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Jan-10-2024 10:19:23
*
*/

#include "gd32l23x.h"
#include "input.h"
#include "led.h"
#include "ds3231.h"
#include "uart_parse.h"


uint8_t gear;
Car_indicators car_led;
uint8_t is_off;
void lock_do()
{
    gpio_bit_set(GPIOC, GPIO_PIN_9);
}

void unlock_do()
{
    gpio_bit_reset(GPIOC, GPIO_PIN_9);
}

void lock_init()
{
    rcu_periph_clock_enable(RCU_GPIOC);
    gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP,  GPIO_PIN_9);
    //gpio_bit_reset(GPIOC, GPIO_PIN_9);
    //unlock_do();
}



uint8_t ctrl_d;
uint8_t ret_status;
uint8_t recoil_energy;
uint8_t throttle_open;


BAT_TYPE bat_up;
BAT_TYPE bat_down;

uint8_t bat_fault;
uint8_t motor_fault;
uint8_t ecu_fault;

void input_init()
{
    bat_fault = 0;
    motor_fault = 0;
    ecu_fault = 0;
    ret_status = 0;
    recoil_energy = 0;
    throttle_open = 0;
    ctrl_d = 0xFF;
    //rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOA);
    //rcu_periph_clock_enable(RCU_GPIOD);
    //rcu_periph_clock_enable(RCU_GPIOB);
    /*DIANCHI_IN;
    JIYOU_IN;
    DIANMENSUO_IN;
    YINGING_IN;
    ONE_IN;
    TWO_IN;
    THREE_IN;
    FOUR_IN;
    FIVE_IN;
    N_IN;
    P_IN;
    SHIKUO_IN;
    WUDENG_IN;
    DAOCHE_IN;*/
    YOUZHUAN_IN;
    ZUOZHUAN_IN;
    DADENG_IN;
    //ABS_IN;
}
#ifdef CHECK_INPUT_ACC

void ble_off();
void wait_acc(void)
{
    volatile uint8_t i = 0;
    volatile uint8_t j, k, l;
    //log_debug("wait acc...\n");
    rcu_periph_clock_enable(RCU_GPIOC);
    ble_off();
    DIANMENSUO_IN;
    BL_CTRL_OFF;

    do {
        if(!(GPIO_ISTAT(GPIOC) & (GPIO_PIN_5))) {
            //log_debug("acc low\n");
            i = 0;

        } else {
            //log_debug("acc high 1\n");
            if(i++ > 10) {
                //log_debug("acc high\n");
                break;
            }

            //i=0;
        }

        for(j = 0; j < 10; j++)for(l = 0; l < 100; l++)for(k = 0; k < 100; k++);

        //vTaskDelay(2 / portTICK_RATE_MS);
        if(is_wwdgt_can_update() == SET) {
            wwdgt_counter_update(0x7F);
            //log_debug("\r\n === feed wwdgt === \r\n");
            //xHandle = xTaskGetCurrentTaskHandle();
            //log_debug("Task stack high water mark: %lu %zu %d\n",
            //uxTaskGetStackHighWaterMark(xHandle),xPortGetMinimumEverFreeHeapSize(),xPortGetFreeHeapSize());
        }
    } while(1);
}



void input_acc_check(void)
{
    static uint8_t acc_check_off = 0;

    if(!(GPIO_ISTAT(GPIOC) & (GPIO_PIN_5)))  {
        if(acc_check_off > 12) {
            is_off = 1;
            //BL_LCD_OFF;

            if(is_wwdgt_can_update() != SET) {
                wwdgt_counter_update(0x40);
            }

            while(1);

            //todo: all led off

        } else {
            acc_check_off++;
        }

    } else {
        if(acc_check_off == 0) {
            if(is_off) {
                is_off = 0;
                //BL_LCD_ON;
                //vTaskDelay(100 / portTICK_RATE_MS);
                //gpio_bit_set(GPIOC, GPIO_PIN_7);
                //todo: all led on
            }

        } else {
            acc_check_off--;
        }
    }
}
#endif
void input_handle(void)
{
    static uint8_t t = 0;
    uint8_t ready = 1;
#ifdef  ENABLE_JIHUO_FUNC

    if(is_daoqi()) { //试用到期
        //JIYOU_ON;
        //DIANCHI_ON;
        GEAR_X_OFF;
        WUDENG_OFF;
        YOUZUAN_OFF;
        ZUOZUAN_OFF;
        GEAR_OFF;
        ECU_ERROR_OFF;
        MOTOR_ERROR_OFF;
        DADENG_OFF;
        SHIKUO_OFF;
        //lock_do();
        return;
    }

#endif

    if(motor_fault) {
        MOTOR_ERROR_ON;
        ready = 0;

    } else {
        MOTOR_ERROR_OFF;
    }

    if(ecu_fault) {
        ECU_ERROR_ON;
        ready = 0;

    } else {
        ECU_ERROR_OFF;
    }

    if(bat_fault) {
        BAT_ERROR_ON;
        ready = 0;

    } else {
        BAT_ERROR_OFF;
    }

    if((bat_up.status & 1) || (bat_down.status & 1)) {
        LOW_POWER_ON;
        ready = 0;

    } else if(((bat_up.sig & 2) && (bat_up.soc <= 150)) || (bat_down.sig & 2) && (bat_down.soc <= 150)) {
        t++;

        if(t > 5) {
            LOW_POWER_ON;

            if(t >= 9) {
                t = 0;
            }

        } else {
            LOW_POWER_OFF;
        }

    } else {
        LOW_POWER_OFF;
    }

    /*if(!(GPIO_ISTAT(GPIOC) & (GPIO_PIN_4)))  {//1
        GEAR_1_ON;
        GEAR_N_OFF;
        //GEAR_ON;

    } else if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_7))) {//2
        GEAR_2_ON;
        GEAR_N_OFF;
        //GEAR_ON;

    } else if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_6))) {//3
        GEAR_3_ON;
        GEAR_N_OFF;
        //GEAR_ON;

    } else if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_5))) {//4
        GEAR_4_ON;
        GEAR_N_OFF;
        //GEAR_ON;

    } else if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_4))) {//5
        GEAR_5_ON;
        GEAR_N_OFF;
        //GEAR_ON;

    } else if(!(GPIO_ISTAT(GPIOD) & (GPIO_PIN_8))) {//P
        GEAR_6_ON;
        GEAR_N_OFF;
        //GEAR_ON;;

    } else if(!(GPIO_ISTAT(GPIOD) & (GPIO_PIN_9))) {//N
        GEAR_N_ON;
        //GEAR_OFF;

    } else {
        GEAR_X_OFF;
        //GEAR_OFF;
    }*/

    /*if(!(GPIO_ISTAT(GPIOB) & (GPIO_PIN_14))) {//JIYOU
        LOW_POWER_ON;

    } else {
        LOW_POWER_OFF;
    }*/

    /*if(!(GPIO_ISTAT(GPIOB) & (GPIO_PIN_10))) {//YINQING
        YINGING_ON;

    } else {
        YINGING_OFF;
    }*/

    /*if(!(GPIO_ISTAT(GPIOC) & (GPIO_PIN_6))) {//DADENG
        //DIANCHI_ON;
    #define DEBUG_ABS
    #ifdef DEBUG_ABS
        ABS_OFF;
    #warning("DEBUG_ABS");
    #else
        ABS_ON;
    #endif

    } else {
        //DIANCHI_OFF;
        ABS_OFF;
    }*/

    /*if((GPIO_ISTAT(GPIOD) & (GPIO_PIN_8))) {//P
        GEAR_5_ON;
        GEAR_ON;;

    } else {
        P_OFF;
    }*/

    if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_5))) {//DADENG
        DADENG_ON;

    } else {
        DADENG_OFF;
    }

    /*if((GPIO_ISTAT(GPIOC) & (GPIO_PIN_5))) {//SHIKUO
        SHIKUO_ON;

    } else {
        SHIKUO_OFF;
    }*/

    /*if((GPIO_ISTAT(GPIOB) & (GPIO_PIN_0))) {//WUDENG
        WUDENG_ON;

    } else {
        WUDENG_OFF;
    }*/

    /*if(!(GPIO_ISTAT(GPIOB) & (GPIO_PIN_1))) {
        car_led.Front_fog_light = 1;

    } else {
        car_led.Front_fog_light = 0;
    }*/

    if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_3))) {//YOUZHUAN
        YOUZUAN_ON;

    } else {
        YOUZUAN_OFF;
    }

    if(!(GPIO_ISTAT(GPIOA) & (GPIO_PIN_4))) {//ZUOZHUAN
        ZUOZUAN_ON;

    } else {
        ZUOZUAN_OFF;
    }

    if(((ret_status & 0x9) != 0) || (bat_up.status & 1) || (bat_down.status & 1) || (motor_fault)) {
        MODE_OFF;
        READY_OFF;

    } else {
        if(ctrl_d == 0) {
            MODE_P;
            READY_OFF;
            //MODE_P;
            //READY_ON;

        } else if(ctrl_d == 1) {
            MODE_1;

            if(ready) {
                READY_ON;

            } else {
                READY_OFF;
            }

        } else if(ctrl_d == 2) {
            MODE_2;

            if(ready) {
                READY_ON;

            } else {
                READY_OFF;
            }

        } else if(ctrl_d == 3) {
            MODE_3;

            if(ready) {
                READY_ON;

            } else {
                READY_OFF;
            }

        } else if(ctrl_d == 100) {
            MODE_R;

            if(ready) {
                READY_ON;

            } else {
                READY_OFF;
            }

        } else {
            MODE_OFF;
            READY_OFF;
        }
    }

    if(bat_up.sig & 2) {
        bat_show(bat_up.soc / 10, 0);

    } else {
        bat_show(0xFF, 0);
    }

    if(bat_down.sig & 2) {
        bat_show(bat_down.soc / 10, 1);

    } else {
        bat_show(0xFF, 1);
    }

    if(throttle_open == 0) {
        LED_OFF(161);
        LED_OFF(162);
        LED_OFF(163);
        LED_OFF(164);
        LED_OFF(165);
        LED_OFF(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 10) {
        LED_ON(161);
        LED_OFF(162);
        LED_OFF(163);
        LED_OFF(164);
        LED_OFF(165);
        LED_OFF(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 20) {
        LED_ON(161);
        LED_ON(162);
        LED_OFF(163);
        LED_OFF(164);
        LED_OFF(165);
        LED_OFF(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 30) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_OFF(164);
        LED_OFF(165);
        LED_OFF(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 40) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_OFF(165);
        LED_OFF(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 50) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_ON(165);
        LED_OFF(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 60) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_ON(165);
        LED_ON(166);
        LED_OFF(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 70) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_ON(165);
        LED_ON(166);
        LED_ON(167);
        LED_OFF(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 80) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_ON(165);
        LED_ON(166);
        LED_ON(167);
        LED_ON(168);
        LED_OFF(169);
        LED_OFF(170);

    } else if(throttle_open <= 90) {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_ON(165);
        LED_ON(166);
        LED_ON(167);
        LED_ON(168);
        LED_ON(169);
        LED_OFF(170);

    } else {
        LED_ON(161);
        LED_ON(162);
        LED_ON(163);
        LED_ON(164);
        LED_ON(165);
        LED_ON(166);
        LED_ON(167);
        LED_ON(168);
        LED_ON(169);
        LED_ON(170);
    }

    if(recoil_energy == 0) {
        LED_OFF(177);
        LED_OFF(178);
        LED_OFF(179);
        LED_OFF(180);
        LED_OFF(181);
        LED_OFF(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 10) {
        LED_ON(177);
        LED_OFF(178);
        LED_OFF(179);
        LED_OFF(180);
        LED_OFF(181);
        LED_OFF(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 20) {
        LED_ON(177);
        LED_ON(178);
        LED_OFF(179);
        LED_OFF(180);
        LED_OFF(181);
        LED_OFF(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 30) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_OFF(180);
        LED_OFF(181);
        LED_OFF(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 40) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_OFF(181);
        LED_OFF(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 50) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_ON(181);
        LED_OFF(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 60) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_ON(181);
        LED_ON(182);
        LED_OFF(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 70) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_ON(181);
        LED_ON(182);
        LED_ON(183);
        LED_OFF(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 80) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_ON(181);
        LED_ON(182);
        LED_ON(183);
        LED_ON(184);
        LED_OFF(185);
        LED_OFF(186);

    } else if(recoil_energy <= 90) {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_ON(181);
        LED_ON(182);
        LED_ON(183);
        LED_ON(184);
        LED_ON(185);
        LED_OFF(186);

    } else {
        LED_ON(177);
        LED_ON(178);
        LED_ON(179);
        LED_ON(180);
        LED_ON(181);
        LED_ON(182);
        LED_ON(183);
        LED_ON(184);
        LED_ON(185);
        LED_ON(186);
    }
}




